package com.jp.knowledge.activity;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.dmcc.image_preview.ImageFragment;
import com.dmcc.image_preview.ImagePreviewActivity;
import com.jp.knowledge.R;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by 528787067@qq.com on 2017/1/23.
 */

public class UiImagePreviewActivity extends ImagePreviewActivity {

    public static void startActivity(Context mContext, int position, ArrayList<String> extraPics) {
        if(position < 0 || extraPics == null || position >= extraPics.size()) {
            return;
        }
        Intent intent = new Intent(mContext, UiImagePreviewActivity.class);
        intent.putExtra("position", position);
        intent.putExtra("extraPics", extraPics);
        mContext.startActivity(intent);
    }

    @Override
    public int bindLayout() {
        return R.layout.activity_ui_img_preview;
    }

    @Override
    public void initView(View view) {
        super.initView(view);
//        toolbar.setBackgroundColor(getResources().getColor(R.color.image_preview_title_color));
//        viewPager.setOffscreenPageLimit(1);
        TextView switchImageQualityBtn = (TextView) view.findViewById(R.id.switch_image_quality_btn);
        switchImageQualityBtn.setTag(false);
        switchImageQualityBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                boolean isOriginalImg = (boolean) view.getTag();
                List<String> imageUrls = new ArrayList<>();
                if(isOriginalImg) {
                    ((TextView) view).setText("查看原图");
                    for(String imgUrl : extraPics)
                        imageUrls.add(imgUrl.substring(0, imgUrl.lastIndexOf('?')) + "?imageView2/1/interlace/1/format/jpg");
                } else{
                    ((TextView) view).setText("查看压缩图");
                    for(String imgUrl : extraPics)
                        imageUrls.add(imgUrl.substring(0, imgUrl.lastIndexOf('?')) + "?imageView2/1/interlace/1");
                }
                extraPics.clear();
                extraPics.addAll(imageUrls);

                fragmentMap.clear();
                mImageViewAdapter.notifyDataSetChanged();

                view.setTag(!isOriginalImg);
            }
        });
    }
}
